<?php defined( '_VALID_MOS' ) or die( 'Direct Access to this location is not allowed.' );

/* JoomLeague League Management and Prediction Game for Joomla!
 * @version $Id$
 * @package Joomleague
 * @copyright Copyright (C) 2007  JoomLeague
 * @license http://www.gnu.org/copyleft/gpl.html GNU/GPL, see _joomleague_license.txt
 *  
 * Homepage: http://www.joomleague.de
 * Support: htt://www.joomleague.de/forum/
 * 
 * This file is part of JoomLeague.
 *  
 * JoomLeague is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * Please note that the GPL states that any headers in files and
 * Copyright notices as well as credits in headers, source files
 * and output (screens, prints, etc.) can not be removed.
 * You can extend them with your own credits, though...
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 *
 * The "GNU General Public License" (GPL) is available at
 * http://www.gnu.org/copyleft/gpl.html.
 */

  /**
   * returns team history ordered by seasons
   *
   * @param int $teamId
   * @param string $season_ordering
   * @return array
   */
  function getTeamHistory( $teamId, $season_ordering = "ASC" )
  {
  	global $database;
  	$query = sprintf( " SELECT tt.id as ttid,
                                   tt.picture, 
                                   tt.description, 
                                   tt.project_id AS projectid, 
                                   tt.team_id, 
                                   p.name as projectname 
                            FROM #__joomleague AS p, #__joomleague_team_joomleague AS tt
                           WHERE team_id = %s AND p.published = 1 
                             and p.id = tt.project_id ORDER BY p.id %s", $teamId, $season_ordering );

    $database->setQuery( $query );
    return $database->loadObjectList();
  }

  function getSeasonTeamCalendar( $teamId, $seasonId, $teams_as_referees = false )
  {
    global $database;
    
    $query = " SELECT m.match_id, m.match_date,
                      m.alt_decision,
                      m.matchpart1_result, m.matchpart2_result,
                      m.matchpart1_result_decision, m.matchpart2_result_decision,
                      t1.id as homeId, t1.name as homeName,
                      t2.id as awayId, t2.name as awayName,
                      pg.name as playgroundName,
                      p.name as projectName ";
    if ( $teams_as_referees ) {
      $query .= ", ref.id as refereeId, ref.name AS refereeName";
    }
    else {
      $query .= ", ref.id as refereeId, CONCAT( ref.firstname, ' ', ref.lastname ) AS refereeName ";    
    }
    
    $query .= " FROM #__joomleague_matches AS m 
               INNER JOIN #__joomleague_teams AS t1 ON t1.id = m.matchpart1
               INNER JOIN #__joomleague_teams AS t2 ON t2.id = m.matchpart2
               LEFT  JOIN #__joomleague_playgrounds AS pg ON pg.id = m.playground_id
               INNER JOIN #__joomleague AS p ON p.id = m.project_id
               INNER JOIN #__joomleague_seasons AS s ON s.id = p.season_id ";
    
    if ( $teams_as_referees ) {
      $query .= "LEFT JOIN #__joomleague_teams AS ref ON ref.id = m.referee_id ";
    }
    else {
      $query .= "LEFT JOIN #__joomleague_referees AS ref ON ref.id = m.referee_id ";   
    }
    
    $query .= " WHERE p.published = 1 
                AND ( t1.id = $teamId OR t2.id = $teamId ) ";
    
    if ( $seasonId ) {
    	$query .= " AND s.id = " . $seasonId;
    }
                 
    $query .= " ORDER BY m.match_date ASC ";

    $database->setQuery( $query );
    // echo $database->getQuery();
    return $database->loadObjectList();
  }
  
  function getSeasonTeamRefCalendar( $teamId, $seasonId )
  {
    global $database;
    
    $query = " SELECT m.match_id, m.match_date,
                      m.alt_decision,
                      m.matchpart1_result, m.matchpart2_result,
                      m.matchpart1_result_decision, m.matchpart2_result_decision,
                      t1.id as homeId, t1.name as homeName,
                      t2.id as awayId, t2.name as awayName,
                      pg.name as playgroundName,
                      p.name as projectName ";
    $query .= ", ref.id as refereeId, ref.name AS refereeName";

    
    $query .= " FROM #__joomleague_matches AS m 
               INNER JOIN #__joomleague_teams AS t1 ON t1.id = m.matchpart1
               INNER JOIN #__joomleague_teams AS t2 ON t2.id = m.matchpart2
               LEFT  JOIN #__joomleague_playgrounds AS pg ON pg.id = m.playground_id
               INNER JOIN #__joomleague AS p ON p.id = m.project_id
               INNER JOIN #__joomleague_seasons AS s ON s.id = p.season_id ";
    $query .= "LEFT JOIN #__joomleague_teams AS ref ON ref.id = m.referee_id ";
    
    $query .= " WHERE p.published = 1 
                AND ref.id = $teamId ";
    
    if ( $seasonId ) {
      $query .= " AND s.id = " . $seasonId;
    }
                 
    $query .= " ORDER BY m.match_date ASC ";

    $database->setQuery( $query );
    // echo $database->getQuery();
    return $database->loadObjectList();
  }
  
  
	function showteaminfo( $overall_config )
	{	
		global $my, $mainframe, $database;
		global $joomleague, $jlgConfig, $htmlHelper;
		
    $tid = intval( mosGetParam($_REQUEST, 'tid', 0) );   // teamid

		$showTeam_config=$joomleague->getTemplateConfig("show_team_info.tpl");
		$mainframe->setPageTitle(_JL_PAGE_TITLE_TEAMINFO);
		//    $mainframe->appendPathWay(_JL_PAGE_TITLE_TEAMINFO);
	
		$team = new JL_Team( $database );
		// know tid is the TEAM ID in this case, NOT project specific teamtool ID
		$team->load($tid);

		// with this theam id we might get the club information
		if($team->club_id > 0){
			$club = new Club($database);
			$club->load($team->club_id);
		}
		
		if ($showTeam_config['ordering_teams_seasons'] == "1") $season_ordering = "DESC";
		else $season_ordering = "ASC";

		$rows = getTeamHistory( $tid );
	  
		// for season calendar
		if ($showTeam_config['show_team_season_calendar'] == "1") 
		{
		  $seasonGames = getSeasonTeamCalendar( $tid, $joomleague->season_id, $joomleague->teams_as_referees );
		  
		  if ( $joomleague->teams_as_referees ) {
        $refseasongames = getSeasonTeamRefCalendar( $tid, $joomleague->season_id );		  	
		  }
		}
		require(getTemplateFile(JL_TEMPLATE_PATH, 'show_team_info.tpl.php'));
	}
?>
